# -*- coding: utf-8 -*-

class Items < Control
  
  def initialize()
    super("items")
  end
  
  def add_page_values(values)
    
    site = Sites.new()
    
    values = site.page_default_settings(values)
    #values["titlebar"] = @title + values["title"]
    
    return values
    
  end
  
  def customize_edit_values(values)
    
    values["contents"]["type"] = "textarea"
    values["contents"]["val"] = values["contents"]["val"].gsub("&","&amp;")
    values["visible"] = set_edit_value("select", "visible", make_yes_no_form("visible", values["visible"]["val"]))
    values["foot"] = set_edit_value("text", "edit_date", values["edit_date"]["val"])
    
    #print get_active_category()
    
    if values["id"]["val"].to_i != 0 then
      default = values["category_id"]["val"]
    else
      default = $_POST["category_id"].to_s
      values["foot"]["val"] = Date.today.to_s
    end
    
    ctg = Categorys.new()
    #ctg.get_select_form(default)
    values["category_id"] = set_edit_value("select", "category_id", ctg.get_select_form(default))
    
    file = Files.new("images")
    values["file_list"] = set_edit_value("select", "file_list", file.get_file_select_form())
    
    
    return values
    
  end
  
  def customize_apply_values(values)
    values["edit_date"] = values["foot"]
    return values
  end
  
  def show_edit_form(values)
    
    html = @vw.load_edit_form(values, $templete_dir + "contents_item_edit.html")
    #html += $_POST.to_s
    return html
    
  end
  
  def customize_main(mode, id)
    
    html = ""
    ctg = Categorys.new()
    
    if !$_GET.has_key?("c") then
      
      values = {}
      values["pages_not_visible"] = ctg.get_link_of_pages_not_visible()
      #print values.to_s
      html = @vw.load(values, $templete_dir + "contents_manage.html")
      #open($templete_dir + "contents_manage.html") {|byte|
      #  html += byte.read
      #}
      @title = $msg_site_management_menu + " - "
      
    else
      
      html = self.get_page_title()
      html = "<div class='title'>#{ctg.get_page_name()}</div>"
      
      html += super(mode, id)
      
    end
    
    #print "Items - customize_main()"
    return html
    
  end
  
  def get_page_title()
    
    html = ""
    ctg = Categorys.new()
    
    if ctg.get_page_num() != 0 then
      html += "<div class='title'>#{ctg.get_page_name()}</div>"
      @title = "#{ctg.get_page_name()} - "
    else
      @title = ""
    end
    
    return html
    
  end
  
  def get_item_new()
    
    values = @mdl.get_data_by_id(0)
    values = add_type_to_value(values)
    values = customize_edit_values(values)
    html = @vw.load_edit_form(values, $templete_dir + "contents_item_add.html")
    #values.each{|row,val|
    #  html += "<div>#{row}:#{val}</div>"
    #}
    return html
    
  end
  
  def get_items()
    
    html = self.get_page_title()
    
    #values = @mdl.get_data()
    values = self.get_list_values()
    
    values.each {|row|
      html +=<<EOF
<div class="item" id="item_#{row["id"]}">
  #{get_item_view(row)}
</div>
EOF
      #html += @vw.load(row, $templete_dir + "contents_item_view.html")
    }
    
    html = self.exchange_title_to_file("index", html)
    
    return html
    
  end
  
  def get_item_view(row)
    
    row["foot"] = row["edit_date"]
    
    if row["visible"] != "false" then
      html = @vw.load(row, $templete_dir + "contents_item_view.html")
    else
      html = @vw.load(row, $templete_dir + "contents_item_no_title.html")
    end
    
    return html
    
  end
  
  def get_list_values()
    
    ctg = get_active_category().to_i
    
    status = "category_id = #{ctg}"
    #status = "category_id = #{ctg}"
    order = "edit_date DESC"
    
    return @mdl.get_data_by_status_with_order(status, order)
    
  end
  
  def get_active_category()
    
    obj = Categorys.new()
    
    if $_GET.has_key?("c") then
      ctg = $_GET["c"]
    else
      ctg = obj.get_min_id()
      #ctg = 0
    end
    
    return ctg
    
  end
  
  def get_item_list(row)
    
    row["foot"] = row["edit_date"]
    html = @vw.load(row, $templete_dir + "contents_item_list.html")
    
    return html
    
  end
  
  def customize_list_table(fields, values)
  
    html = ""
    #values = @mdl.get_data()
    
    values.each {|row|
      html +=<<EOF
<div class="item" id="item_#{row["id"]}">
  #{get_item_list(row)}
</div>
EOF
      #html += @vw.load(row, $templete_dir + "contents_item_list.html")
    }
    
    html = self.exchange_title_to_file("item", html)
    
    return html
    
  end
  
  def get_sidebar()
    
    sidebar_id = -10
    
    status = "category_id = #{sidebar_id}"
    order = "edit_date DESC"
    
    values = @mdl.get_data_by_status_with_order(status, order)
    
    html = ""
    
    values.each {|row|
      html +=<<EOF
<div class="item">
  #{@vw.load(row, $templete_dir + "sidebar_item_view.html")}
</div>
EOF
    }
    
    html = self.exchange_title_to_file("index", html)
    
    return html
    
  end
  
  def get_new_form()
    
    row = {}
    row["category_id"] = get_active_category().to_i
    if row["category_id"] == -1 then
      html = self.get_del_form()
    else
      html = @vw.load(row, $templete_dir + "contents_item_new.html")
    end
    
    return html
    
  end
  
  def get_del_form()
    #row = @mdl.get_data_by_id(0)
    row = {}
    row["category_id"] = get_active_category()
    #html = @vw.load(row, $templete_dir + "contents_item_new.html")
    html = @vw.load(row, $templete_dir + "contents_item_del.html")
    return html
  end
  
  def empty_the_trash()
    
    sql = "DELETE from items WHERE category_id = -1"
    #print sql
    @mdl.exec(sql)
    
  end
  
  def exchange_title_to_file(type, html)
    
    # type = "index" or "item" or "file"
    
    ctg = Categorys.new()
    
    pages = ctg.get_data_for_html_files()
    
    pages.each {|row|
      
      if type == "index" then
        tmp = "index.rb?c=#{row["id"]}"
      elsif type == "item" then
        tmp = "items.rb?c=#{row["id"]}"
      else
        tmp = row["filename"]
      end
      
      #print tmp
      
      html.gsub!("[[#{row["name"]}]]", tmp)
      
    }
    
    return html
    
  end
  
  def proxy()
    
    html_open()
    
    mode = @params["mode"]
    id = @params["id"]
    
    if mode == "new" then
      #html = get_item_new()
    
    elsif mode == "apply" then
      
      html = ""
      if @params["title"] == "" then
        html += $msg_items_title_empty
      else
        values = customize_apply_values(@params)
        $_GET["c"] = values["category_id"]
        @mdl.apply(values)
      end
      
      html += make_page_contents("", id)
      #values = @mdl.get_data_by_id(id)
      #if id.to_i == 0 then
      #else
      #  html = @vw.load(values, $templete_dir + "contents_item_list.html")
      #end
      #html = customize_main(mode, id)
    
    elsif mode == "view" then
      if id.to_i == 0 then
        html = ""
      else
        values = @mdl.get_data_by_id(id)
        html = get_item_list(values)
      end
      html = self.exchange_title_to_file("item", html)
      
    elsif mode == "delete" then
      empty_the_trash()
      $_GET["c"] = "-1"
      html = self.get_page_title()
      html += make_page_contents(mode, id)
      html += $msg_items_trash_empry_complete
      
    elsif mode == "sidebar" then
      html = self.get_sidebar()
    else
      html = make_page_contents(mode, id)
      
    end
    
    print html
    #print print_params()
    
    terminate()
    html_close()
    
  end
  
end
